<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */
namespace App\Libs\Redis;

use Illuminate\Support\Facades\Redis;

class RedisLibs
{
    private $redis;

    static $seconds = 0;

    /**
     * 实力化
     * RedisLibs constructor.
     *
     * @param null $name
     */
    function __construct( $name = NULL )
    {
        $this->redis   = Redis::connection($name);
        self::$seconds = config('page.seconds');
    }

    /**
     * 根据键查询
     *
     * @param $key
     *
     * @return string
     */
    function get( $key )
    {
        return $this->redis->get((string) $key);
    }

    /**
     * 查找
     *
     * @param $key
     *
     * @return int
     */
    function exists( $key )
    {
        return $this->redis->exists((string) $key);
    }

    /**
     * 递增
     *
     * @param $key
     *
     * @return int
     */
    function incr( $key )
    {
        return $this->redis->incr($key);
    }

    /**
     * 递减
     *
     * @param $key
     *
     * @return int
     */
    function decr( $key )
    {
        return $this->redis->decr($key);
    }

    /**
     * 删除
     *
     * @param $key
     *
     * @return int
     */
    function del( $key )
    {
        return $this->redis->del($key);
    }

    /**
     * 查询有效期
     *
     * @param $key
     *
     * @return int
     */
    function ttl( $key )
    {
        return $this->redis->ttl($key);
    }

    /**
     * 设置有效期
     *
     * @param $key
     * @param $seconds
     *
     * @return int
     */
    function expire( $key, $seconds = 0 )
    {
        return $this->redis->expire($key, $seconds ? (int) $seconds : self::$seconds);
    }

    /**
     * 设置缓存
     *
     * @param      $key
     * @param      $value
     * @param null $expireResolution
     * @param null $expireTTL
     * @param null $flag
     *
     * @return mixed
     */
    function put( $key, $value, $expireResolution = NULL, $expireTTL = NULL, $flag = NULL )
    {
        return $this->redis->set($key, $value, $expireResolution, $expireTTL, $flag);
    }

    /**
     * 设置带时效的缓存
     *
     * @param $key
     * @param $seconds
     * @param $value
     *
     * @return int
     */
    function set( $key, $value, $seconds = 0 )
    {
        return $this->redis->setex($key, $seconds ? (int) $seconds : self::$seconds, $value);
    }

    /**
     * 设置永久储存
     *
     * @param $key
     *
     * @return int
     */
    function persis( $key )
    {
        return $this->redis->persist($key);
    }

    /**
     * 存入队列 先进先出
     *
     * @param       $key
     * @param       $values
     *
     * @return int
     */
    function rPush( $key, $values )
    {
        return $this->redis->rpush($key, $values);
    }

    /**
     * 取出队列 先进先出
     *
     * @param $key
     *
     * @return string
     */
    function rPop( $key )
    {
        return $this->redis->rpop($key);
    }

}
